package com.study.ssm.simple;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author zhoujunlin
 * @date 2023年07月20日 18:57
 * @desc
 */
public class MyBeanFactory {

    private static final Properties env = new Properties();

    static {
        try (InputStream inputStream = MyBeanFactory.class.getResourceAsStream("/simple/myApplicationContext.properties")) {
            env.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Dog getDog() {
        Dog dog = null;
        try {
            Class<?> clazz = Class.forName(env.getProperty("dog"));
            dog = (Dog) clazz.newInstance();
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
        return dog;
    }

    public static Animal getAnimal() {
        Animal animal = null;
        try {
            Class<?> clazz = Class.forName(env.getProperty("animal"));
            animal = (Animal) clazz.newInstance();
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
        }
        return animal;
    }

    public static Object getBean(String key) {
        Object obj = null;
        try {
            Class<?> clazz = Class.forName(env.getProperty(key));
            obj = clazz.newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    }

}
